package org.linlinjava.litemall.admin.job;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.task.ScheduledCronTaskRegistrar;
import org.linlinjava.litemall.db.domain.LitemallRewardRule;
import org.linlinjava.litemall.db.service.LitemallRewardRuleService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 奖励工作
 *
 * @author yliyun
 * @description：检测奖励规则过期情况
 * @date 2022/09/14
 */
@Component
public class RewardJob implements Job {
    /**
     * 日志记录器
     */
    private final Log logger = LogFactory.getLog(RewardJob.class);

    /**
     * litemall奖励规则服务
     */
    @Autowired
    private LitemallRewardRuleService litemallRewardRuleService;

    @Autowired
    private ScheduledCronTaskRegistrar scheduledCronTaskRegistrar;

    /**
     * 过期奖励规则
     * 查看奖励规则的过期情况
     */
//    @Scheduled(cron = "0 0 * * * ?")
    //@Scheduled(cron = "0 0 1 * * ?")
    public void expiredRewardRule() {
        logger.info("检测状态为新增和生效");
        Byte[] inStatus = {1,3};
        List<Byte> status = Arrays.asList(inStatus);
        List<LitemallRewardRule> rules = litemallRewardRuleService.listNewAndNormal(null,status);
        List<Integer> ids = rules.stream().map(LitemallRewardRule::getId).collect(Collectors.toList());

        if(ids != null && ids.size() > 0){
            litemallRewardRuleService.updateBatchById((byte)2,ids);
        }
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.expiredRewardRule();
        scheduledCronTaskRegistrar.scheduleJobLog(jobExecutionContext);
    }
}
